package com.fau.cm.action.index;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalResource;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 用户管理action
 * @author liy
 * @datetime 2015年1月6日,下午3:38:17
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class UserAction extends PublicAction implements ModelDriven<MedicalAdmin> {

	private static final long serialVersionUID = -2061560598783331675L;

	private MedicalAdmin admin = new MedicalAdmin();
	
	@Value("${server.local.name}")
	private String urlPath;
	
	/**
	 * @desc 登录
	 * @return String
	 * @author liy
	 * @datetime 2015年1月5日,下午4:13:05
	 */
	@Action(value = "login", results = {@Result(name = SUCCESS, location = "login.jsp")})
	public String login() {
		return SUCCESS;
	}
	
	/**
	 * @desc 进入首页
	 * @return String
	 * @author liy
	 * @datetime 2015年1月5日,下午4:13:21
	 */
	@Action(value = "index", results = {@Result(name = SUCCESS, location = "main.jsp")})
	public String index() {
		int num = this.baseAllService.getUserService().countUnreadNum();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("num", num);
		return SUCCESS;
	}
	
	/**
	 * @desc 加载菜单
	 * @return null
	 * @author liy
	 * @datetime 2015年1月5日,下午4:13:39
	 */
	@Action(value = "loadMenu")
	public String loadMenu() {
		HttpServletResponse response = ServletActionContext.getResponse();
		List<MedicalResource> list = baseAllService.getUserService().loadMenu();
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入消息列表页面
	 * @return String
	 * @author liy
	 * @datetime 2015年1月5日,下午4:13:59
	 */
	@Action(value = "message", results = {@Result(name = SUCCESS, location = "message.jsp")})
	public String message() {
		return SUCCESS;
	}
	
	/**
	 * @desc 删除消息
	 * @return null
	 * @author liy
	 * @datetime 2015年1月5日,下午4:22:06
	 */
	@Action(value = "message_delete")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(admin.getId())){
			this.baseAllService.getUserService().deleteMassage(this.admin.getId());
			this.map.put("success", true);
		}else{
			this.map.put("error", true);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 分页查询消息表
	 * @return null
	 * @author liy
	 * @datetime 2015年1月5日,下午4:30:14
	 */
	@Action(value = "message_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getUserService().findMassageByPage(admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 设置为已读
	 * @return null
	 * @author liy
	 * @datetime 2015年1月5日,下午5:15:55
	 */
	@Action(value = "message_read")
	public String read(){
		this.baseAllService.getUserService().setRead();
		return null;
	}
	
	/**
	 * @desc 查询话务中心
	 * @return null
	 * @author liy
	 * @datetime 2015年1月6日,下午3:45:59
	 */
	@Action(value = "user_find_center")
	public String findCenter() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getUserService().findAllCurSerCenter(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询话务组
	 * @return null
	 * @author liy
	 * @datetime 2014年12月17日,上午10:54:11
	 */
	@Action(value="user_find_group")
	public String findGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String centerId = request.getParameter("centerId");
		list = this.baseAllService.getUserService().findAllCurSerGroup(centerId);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询某组下的话务员
	 * @return null
	 * @author liy
	 * @datetime 2014年12月17日,上午10:58:43
	 */
	@Action(value="user_cur_ser")
	public String curSerView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getUserService().findCurSerByGroupId(admin.getGroupId());
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalAdmin getModel() {
		return admin;
	}
	
	public String getUrlPath() {
		return urlPath;
	}

	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}

}
